//! 设备和私钥
//! 为了一个设备生成临时的私钥
use crypto::{
    digest::Digest,
    sha3::{Sha3, Sha3Mode},
};
use rand::{RngCore, SeedableRng};
use rand_hc::Hc128Rng;
pub struct PrivateKey {
    device_name: String,
    passphrase: String,
    device_tmp_private_key: [u8; 32],
    device_prev_tmp_private_key: [u8; 32],
}

impl PrivateKey {
    pub fn new(device_name: String, passphrase: String) -> Self {
        let device_tmp_private_key = Self::device_tmp_private_key_generate(&device_name);
        let device_prev_tmp_private_key = Self::device_prev_tmp_private_key_generate(&device_name);
        Self {
            device_name,
            passphrase,
            device_tmp_private_key,
            device_prev_tmp_private_key,
        }
    }

    pub fn device_tmp_private_key(&self) -> [u8; 32] {
        self.device_tmp_private_key
    }

    pub fn device_prev_tmp_private_key(&self) -> [u8; 32] {
        self.device_prev_tmp_private_key
    }

    fn device_tmp_private_key_generate(device_name: &str) -> [u8; 32] {
        let mut ha = Sha3::new(Sha3Mode::Keccak256);
        ha.input_str(device_name);
        let mut hash_value = [0u8; 32];
        ha.result(&mut hash_value);
        let mut r = Hc128Rng::from_seed(hash_value);
        let mut ret = [0u8; 32];
        r.fill_bytes(&mut ret);
        ret
    }

    fn device_prev_tmp_private_key_generate(device_name: &str) -> [u8; 32] {
        Self::device_tmp_private_key_generate(device_name)
    }
}
